import fs from "fs";
import path from "path";
import ExcelJS from "exceljs";
import { OutPutInfo } from '../typedef/parseLog/parseLog'


export class FileUtils {
  /**
   * 读取文件，返回满足要求的所有文件路径
   *
   * @param {string} dirName 文件目录
   * @param {(name: string) => boolean} [filter] 文件路径的过滤条件
   * @return {Array<string>}
   */
  static readFilesInDir(dirName: string, filter?: (name: string) => boolean): Array<string> {
    const files: Array<string> = [];
    fs.readdirSync(dirName, { withFileTypes: true }).forEach((dir: any) => {

      const filePath: string = path.join(dirName, dir.name);
      if (dir.isFile()) {
        if (!filter) {
          files.push(filePath);
          return;
        }
        if (filter(dir.name)) {
          files.push(filePath);
        }
        return;
      }
      files.push(...FileUtils.readFilesInDir(filePath, filter));
    });

    return files;
  }
}

export async function ExcelReporter(data: OutPutInfo[], workbook: ExcelJS.Workbook) {

  // const workbook: ExcelJS.Workbook = new ExcelJS.Workbook();
  const sheet: ExcelJS.Worksheet = workbook.addWorksheet();
  const sheet2: ExcelJS.Worksheet = workbook.addWorksheet();
  sheet.name = "编译性能差异对比";
  sheet2.name = "编译告警差异对比"
  sheet.views = [{ state: "frozen", xSplit: 1, ySplit: 1 }];
  sheet2.views = [{ state: "frozen", xSplit: 1, ySplit: 1 }];
  sheet.columns = [
    { header: "应用名称", key: "fileName", width: 50 },
    { header: "应用所用SDK", key: "appType", width: 20 },
    { header: "ArkTS编译时间（旧）", key: "oldCompileArkTSTime", width: 20 },
    { header: "ArkTS编译时间（新）", key: "newCompileArkTSTime", width: 20 },
    { header: "ArkTS编译时间差（ms）", key: "compileArkTSTimeDiff", width: 20 },
    { header: "ArkTS编译时间异常", key: "isCompileArkTSAlarm", width: 20 },
    { header: "总编译时间（旧）", key: "oldSuccessfulTime", width: 20 },
    { header: "总编译时间（新）", key: "newSuccessfulTime", width: 20 },
    { header: "总编译时间差（ms）", key: "successfulTimeDiff", width: 20 },
    { header: "总编译时间异常", key: "isSuccessfulAlarm", width: 20 },
  ];
  sheet2.columns = [
    { header: "应用名称", key: "fileName", width: 50 },
    { header: "应用所用SDK", key: "appType", width: 20 },
    { header: "新sdk编译通过", key: "newType", width: 20 },
    { header: "旧sdk编译通过", key: "oldType", width: 20 },
    { header: "新sdk编译告警", key: "newAlarmAdded", width: 100 },
    { header: "旧sdk编译告警", key: "deleteAlarm", width: 100 },
  ];
  let lineNumber: number = 2;
  let lineNumber2: number = 2;
  data.forEach((item: OutPutInfo) => {
    if (item.oldType === 'success' || item.newType === 'success') {
      sheet.getRow(lineNumber++).values = [
        item.fileName,
        item.appType,
        item.oldCompileArkTSTime,
        item.newCompileArkTSTime,
        item.compileArkTSTimeDiff,
        item.isCompileArkTSAlarm,
        item.oldSuccessfulTime,
        item.newSuccessfulTime,
        item.successfulTimeDiff,
        item.isSuccessfulAlarm
      ];
    }
    sheet2.getRow(lineNumber2++).values = [
      item.fileName,
      item.appType,
      item.newType,
      item.oldType,
      item.newAlarmAdded,
      item.deleteAlarm,
    ];
  });
  // const buffer: NodeJS.ArrayBufferView = (await workbook.xlsx.writeBuffer()) as NodeJS.ArrayBufferView;
  // fs.writeFileSync(outputFile, buffer);
}

